package com.lagou.demo03.dao;

import com.lagou.demo03.model.Student;
import com.lagou.demo03.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentUpdateDaoImp implements StudentUpdateDao {

    @Override
    public Student studentUpdate(Student student , String updateId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //获取数据库连接
            connection = DbUtil.getConnection();

            //准备sql
            String sql = "UPDATE t_student SET id = ?,NAME=?,sex =?,birthday=?,mail=?,note=? WHERE id = ?;";
            //执行sql，并获取结果返回
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,student.getId());
            preparedStatement.setString(2,student.getName());
            preparedStatement.setString(3,student.getSex());
            preparedStatement.setString(4,student.getBirthday());
            preparedStatement.setString(5,student.getMail());
            preparedStatement.setString(6,student.getNote());
            preparedStatement.setString(7,updateId);
            System.out.println(sql);
            //将数据添加至数据库
            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                DbUtil.closeResource(connection,preparedStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
